<?php

namespace app\supplier\controller\setting;

use app\Request;
use app\supplier\controller\Controller;
use app\supplier\model\settings\Shoptime as ShoptimeModel;
use think\exception\HttpException;

/**
 * 营业时间管理
 */
class Shoptime extends Controller
{
	/**
	 * 列表
	 */
	public function index(Request $request)
	{
		$where = $request->param();
		$ShoptimeModel = new ShoptimeModel();
		$list = $ShoptimeModel->getList(10,$this->getSupplierId());
		return $this->renderSuccess('', compact('list'));
	}

	public function create(Request $request)
	{
		try {
			$getqt = $request->param();
			$where = [
				'shop_supplier_id'=>$this->getSupplierId(),
				'time_range'=>1,
				'start_time'=>$getqt['startTime'],
				'end_time'=>$getqt['endTime'],
				'create_time'=>time()
			];
			// 查找重复
			$Cfrs = ShoptimeModel::checkDuplicate(['time_range'=>1,'shop_supplier_id'=>$this->getSupplierId()]);
			if($Cfrs){
				return $this->renderError('存在重复数据');
			}
			// 新增记录
			$result = ShoptimeModel::createData($where);
			return $this->renderSuccess('操作成功', $result);
		} catch (\Exception $exception) {
			return $this->renderError($exception->getMessage());
		}
	}

	public function edit(Request $request)
	{
		$getqt = $request->param();
		$where = [
			'shoptime_id'=>$getqt['shoptime_id']
		];
		// 用户详情
		$model = ShoptimeModel::one($where);
		if($model['shop_supplier_id']!=$this->getSupplierId()){
			return $this->renderError('没有操作权限');
		}
		return $this->renderSuccess('', compact('model'));

	}

	public function update(Request $request)
	{
		$getqt = $request->param();
		$where = [
			'shoptime_id'=>$getqt['shoptime_id'],
			'start_time'=>$getqt['start_time'],
			'end_time'=>$getqt['end_time']
		];
		$model = ShoptimeModel::detail($getqt['shoptime_id']);
		if($model['shop_supplier_id']!=$this->getSupplierId()){
			return $this->renderError('没有操作权限');
		}
		// 修改记录
		$model->save($getqt);
		return $this->renderSuccess('修改成功');
	}

	public function delete(Request $request)
	{
		$params = $request->param();
		// 修改记录
		ShoptimeModel::deleteData($params);
		return $this->renderSuccess('删除成功');
		
	}

}
